<template>
  <div class="y-conter">
    <div class="y-biaoge">
      <h3 class="y-bold">电力监测</h3>
      <!-- 搜索输入框部分 -->
      <el-form
        :inline="true"
        ref="ruleForm"
        :model="ruleForm"
        class="demo-form-inline"
      >
        <el-form-item label="设备名称:">
          <el-select v-model="ruleForm.equipment_name" placeholder="全部">
            <el-option
              v-for="item in tableData"
              :key="item.index"
              :label="item.equipment_name"
              :value="item.equipment_name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search('ruleForm')">搜索</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <div class="y-btn">
          <el-button class="y-btn1-right" type="primary" @click="Newadditions()"
            >新增</el-button
          >
        </div>
      </el-form>
      <!-- 表格部分 -->
      <el-table
        :border="true"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        ref="multipleTable"
        :data="currList"
        highlight-current-row
        tooltip-effect="dark"
        style="width: 98%, text-align: center "
        @selection-change="handleSelectionChange"
      >
        <!-- 复选框 -->
        <el-table-column
          align="center"
          type="index"
          width="100"
          :index="indexFn"
          label="序号"
        ></el-table-column>
        <el-table-column
          label="设备名称"
          prop="equipment_name"
          width="170"
          hight="30px"
          align="center"
        ></el-table-column>
        <el-table-column
          label="设备编号"
          prop="equipment_code"
          width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          label="ip地址"
          prop="equipment_ip"
          align="center"
        ></el-table-column>
        <el-table-column
          label="安装时间"
          prop="equipment_install"
          align="center"
        ></el-table-column>
        <el-table-column
          label="安装地址"
          prop="equipment_addr"
          align="center"
        ></el-table-column>
        <el-table-column
          label="在线状态"
          prop="equipment_status"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <span
              class="el-icon-success"
              style="color: green"
              v-if="scope.row.equipment_status == '0'"
            ></span>
            <span class="el-icon-remove" style="color: red" v-else></span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="y-fenye">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="tableData.length"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import http from "../api";
export default {
  data() {
    return {
      //搜索输入框
      ruleForm: {
        equipment_name: "",
        equipment_type_id: "4",
      },
      //多选表格的复选框
      multipleSelection: [],
      // ====分页=====
      tableData: [], //总数居
      currPage: 1, //当前页
      pageSize: 5, //每页数据量
      currList: [], //当前展示的数据数组
    };
  },
  computed: {},
  methods: {
    // 新增
    Newadditions() {
      this.$router.push({
        name: "新增电力设备",
      });
    },
    //表格编辑按钮
    handleEdit(index, row) {
      this.$router.push({
        name: "编辑电力设备",
        params: row,
      });
    },
    // 分页
    setCurrList(index) {
      this.currList = this.tableData.slice(
        (index - 1) * this.pageSize,
        index * this.pageSize
      );
      // console.log('当前页',index);
    },
    //改变当前页
    changePage(val) {
      this.currPage = val;
      this.setCurrList(val);

      // console.log(val);
    },
    // 表格序号
    indexFn(index) {
      index = index + 1 + (this.currPage - 1) * this.pageSize;
      return index;
    },
    //重置按钮
    resetForm(formName) {
      this[formName] = {};
      this.$refs[formName].resetFields();
    },

    // 表格删除按钮
    handleDelete(index, row) {
      console.log(row);
      http.del(row).then((r) => {
        http
          .system({
            equipment_type_id: "4",
          })
          .then((r) => {
            console.log(r);
            this.tableData = r.data;
            console.log(r.data);
            // console.log(this.tableData);
            this.setCurrList(1);
          });
        console.log(r);
        console.log("单个删除", r.data);
      });
    },

    //选中复选框获取下标
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // let ids = [];
      // this.multipleSelection.map((item) => {
      //   ids.push(item.key);
      // });
      // this.selectedIDs = ids;
      // console.log("你点击的是", this.selectedIDs);
    },

    //搜索输入
    search(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          http.system(this.ruleForm).then((r) => {
            console.log("返回的数据", r);
            this.tableData = r.data;
            this.setCurrList(1);
            console.log(this.ruleForm);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

  },
  //获取数据
  mounted() {
    http
      .system({
        equipment_type_id: "4",
      })
      .then((r) => {
        console.log(r);
        this.tableData = r.data;
        console.log(r.data);

        // console.log(this.tableData);
        this.setCurrList(1);
      });
  },
};
</script>
<style scoped>
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px;
  /* margin-left: 5px; */
}
/* 大容器 */
.y-conter {
  width: 100%;
  height: 640px;
  background-color: white;
  overflow: hidden;
  overflow-y: scroll;
  margin-top: 15px;
}
/* 对话框表单 */
.y-form-left {
  display: inline-block;
}
.y-form-right {
  display: inline-block;
  margin-left: 20px;
}
/* 表格 */
.y-biaoge {
  margin-top: 20px;
  margin-left: 20px;
}
/* 右侧新增导出按钮 */
.y-btn {
  position: absolute;
  top: 190px;
  left: 100%;
}

/* 新增按钮 */
.y-btn1-right {
  position: absolute;
  top: -53px;
  right: 10px;
}
/* 所有按钮 */
.el-button {
  height: 30px;
  line-height: 0px;
}
/* 标题字体 */
.y-bold {
  font-weight: bold;
}

.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
  margin-left: 5px;
}
.el-input >>> .el-input__inner {
  width: 178px;
  height: 30px;
  margin-left: 5px;
}
.el-range-editor {
  width: 160px;
  height: 30px;
  padding-left: 10px;
  margin-left: 5px;
}
.el-input >>> .el-range__icon {
  line-height: 0px;
  padding-left: 10px;
}
.el-range-editor >>> .el-input__inner {
  width: 200px;
  height: 30px;
  margin-left: 5px;
  margin-top: 10px;
}
.y-fenye {
  float: right;
  margin-top: 20px;
}

</style>
